import request from '@/config/axios'
import { SpuStatus } from '../client-mall/product/spu'

export enum CourseStatus {
  'ENABLE' = 1,
  'DISABLE' = 2
}

export type CourseVO = {
  id: string
  skuId: string
  spuId: string
  courseName: string
  courseCoverUrl: string
  // courseStatus: CourseStatus
  spuStatus: SpuStatus
  isRecommend: boolean
  // tags: string[]
  validDays: number
  sortNo: number
  createTime: number
  updateTime: number
}

// 商品属性
type SpuInfo = {
  spuStatus: SpuStatus
  virtualSalesCount: number
}

// sku属性
type SkuInfo = {
  skuPrice: number
}

export type CourseDetailVO = CourseVO & {
  courseDetail: string
  spuStatus: number
  spuVirtualSalesCount: number
  skuPrice: number
}

export type CourseDTO = Partial<Omit<CourseVO, 'id'>>

export type CourseQueryDTO = Partial<Pick<CourseVO, 'courseName' | 'spuStatus'>>

export type CourseCreateDTO = {
  courseName: string
  courseCoverUrl: string
  isRecommend: boolean
  validDays: number
  sortNo: number
  courseDetail: string
  spuInfo: {
    status: number
    virtualSalesCount: number
  }
  skuInfo: {
    price: number
  }
}

export type CourseUpdateDTO = {
  id: string
  courseName: string
  courseCoverUrl: string
  isRecommend: boolean
  validDays: number
  sortNo: number
  courseDetail: string
  spuInfo: {
    id: string
    status: number
    virtualSalesCount: number
  }
  skuInfo: {
    id: string
    price: number
  }
}

export type MemberCourseMappingVO = {
  nickname: string
  avatar: string
  courseName: string
  courseCoverUrl: string
  validDays: number
  createTime: number
  updateTime: number
}

const baseURL = '/train/course'

// 查询列表
export const getPage = async (params: PageParam & CourseDTO) => {
  return await request.post<PageResult<CourseVO & SpuInfo & SkuInfo>>({
    url: `${baseURL}/page-course`,
    data: params
  })
}

export const getPlatformCoursePage = (
  params: PageParam & {
    courseName?: CourseVO['courseName']
    courseStatus?: CourseVO['spuStatus']
  }
) =>
  request.post<PageResult<CourseVO & SpuInfo & SkuInfo>>({
    url: `${baseURL}/page-platform-course`,
    data: params
  })

// 查询详情
export const get = async (id: string): Promise<CourseDetailVO> => {
  return await request.get<CourseDetailVO>({
    url: `${baseURL}/get-course-by-id`,
    params: {
      id
    }
  })
}

// 获取课程开通记录
export const getMemberCourseMappingPage = async (params: PageParam) => {
  return await request.post<PageResult<MemberCourseMappingVO>>({
    url: `${baseURL}/member-page-course`,
    data: params
  })
}

// 新增
export const create = async (data: CourseCreateDTO) => {
  return await request.post<boolean>({ url: `${baseURL}/create`, data })
}

// 更新
export const update = async (data: CourseUpdateDTO) => {
  return await request.post<boolean>({ url: `${baseURL}/update`, data })
}

// 删除
export const deleteById = async (id: string) => {
  return await request.delete<boolean>({ url: `${baseURL}/delete?id=` + id })
}
